package com.hdw.auth.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hdw.auth.api.CoreAuthorizationService;
import com.hdw.auth.api.UserService;
import com.hdw.auth.utils.Ognlutis;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hdw.auth.bean.vo.LayMsg;
import com.hdw.auth.bean.vo.LayTree;
import com.hdw.auth.bean.vo.Msg;
import com.hdw.auth.bean.model.SysUsers;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author GuoPeng
 * @Title: UserController
 * @Description: 用户控制器
 * @date 2019/5/11 10:34
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/permission")
public class UserController {

    @Resource
    private CoreAuthorizationService coreAuthorizationService;

    @Resource
    private UserService userService;


    //以下为用户的增删改查
    @RequestMapping("/getuserlist")
    @ResponseBody
    public LayMsg getuserlist(String LoginKey, Integer page, Integer limit) {
        return userService.getuserList( page, limit);
        /*if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            return userService.getuserList( page, limit);
        } else {
            return LayMsg.fail();
        }*/
    }


    @ResponseBody
    @RequestMapping("/updateUser")
    public Msg updateUser(String LoginKey, String user) {
        Gson gson = new Gson();
        SysUsers sysUsers = (SysUsers) gson.fromJson(user, new TypeToken<SysUsers>() {
        }.getRawType());
        Msg msg = coreAuthorizationService.autLoginToken(LoginKey);
        if (msg.getCode() == 100) {
            return userService.updateuser(sysUsers);
        } else {
            return msg;
        }
    }


    @RequestMapping("/userLock")
    @ResponseBody
    public Msg userLock(String LoginKey, Integer id, Boolean lock) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            SysUsers sysUsers = new SysUsers();
            sysUsers.setId(id);
            sysUsers.setLocked(lock);
            return userService.userLock(sysUsers);
        } else {
            return Msg.fail();
        }
    }

    @RequestMapping("/delectuser")
    @ResponseBody
    public Msg delectuser(String LoginKey, String user) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            List<Map> maps = (List) gson.fromJson(user, new TypeToken<List<Map>>() {
            }.getRawType());
            for (int i = 0; i < maps.size(); i++) {
                Integer id=  Ognlutis.getNumber("id", maps.get(i)).intValue();
                Msg delectuser = userService.delectuser(id);
            }
            return  Msg.success();
        }

        return Msg.fail();
    }


    /*树图*/
    @RequestMapping("/selectTree")
    @ResponseBody
    public LayTree selectTree(String LoginKey, Integer id){
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
          return userService.selectTree(id);
        }
        return LayTree.createLayTree();
    }
    /*树图*/
    @RequestMapping("/selectTreeAll")
    @ResponseBody
    public LayTree selectTreeAll(String LoginKey){
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            return userService.SelectreeAll();
        }
        return LayTree.createLayTree();
    }


}
